package com.thinta.wind.ui.home.fragment

import androidx.core.content.ContextCompat
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.SimpleItemAnimator
import com.thinta.wind.JetConstant
import com.thinta.wind.R
import com.thinta.wind.base.BaseFragment
import com.thinta.wind.databinding.StarFragmentBinding
import com.thinta.wind.entity.User
import com.thinta.wind.ui.home.adapter.HotComAdapter
import com.thinta.wind.ui.home.repository.HotComFactory
import com.thinta.wind.ui.home.repository.HotComRepository
import com.thinta.wind.ui.home.viewmodel.HotComViewModel
import com.thinta.wind.utils.PreferenceUtils

class StarFragment : BaseFragment<HotComViewModel, StarFragmentBinding>() {

    companion object {
        fun newInstance() = StarFragment()
    }

    override fun getModelFactory(): ViewModelProvider.NewInstanceFactory = HotComFactory(
        HotComRepository()
    )

    private val mHotComAdapter : HotComAdapter by lazy { HotComAdapter() }

    override fun viewModelClass(): Class<HotComViewModel> = HotComViewModel::class.java

    override fun getLayoutId(): Int = R.layout.star_fragment

    override fun afterCreate() {
        dataBinding?.starList?.layoutManager = LinearLayoutManager(context)
        dataBinding?.starList?.isNestedScrollingEnabled = true
        (dataBinding?.starList?.itemAnimator as SimpleItemAnimator).supportsChangeAnimations = false
        dataBinding?.starList?.adapter = mHotComAdapter
        val divider =  DividerItemDecoration(context, DividerItemDecoration.VERTICAL)
        context?.let {
            ContextCompat.getDrawable(it, R.drawable.divider_selector)?.let { it1 ->
                divider.setDrawable(
                    it1
                )
            }
        }
        dataBinding?.starList?.addItemDecoration(divider)
        initData()

    }
    private fun initData(){
        val user = PreferenceUtils.getObject(PreferenceUtils.USER_INFO, User::class.java) as User
        viewModel?.getHotData(JetConstant.HOT_STAR, JetConstant.PAGE_SIZE, user.id)
        viewModel?.result?.observe(this, {
            mHotComAdapter.submitData(lifecycle, it)
        })
    }

}
